//
//  AsmanLiteViewModel.swift
//  vr
//
//  Created by 李旭峰 on 2020/7/17.
//

import UIKit
import RxSwift
import RxCocoa
import TXLiteAVSDK_TRTC

typealias AsmanLiteRejectCallBack = (_ success:Bool,_ error:String?)->Void
typealias AsmanRtcAnswerCallBack = (_ success:Bool,_ error:String?)->Void

class AsmanLiteViewModel: BaseViewModel {
    struct InitialParams {
        let userId:String
        let accountId:String?
        let roomId:UInt32
        let headUrl:String
        let name:String
        let schemeId:String
        let groupId:String
        var vrType:Int?
    }
    let userId:String
    var accountId:String?
    let roomId:UInt32
    let headUrl:String
    let name:String
    let schemeId:String
    let groupId:String
    var vrType:Int?
    var token:String
    var sdkAppId:String
    var userSig:String
    
    //TODO: 换成这个接口的请求 /web/scheme/search_scheme_by_id
    private lazy var loadSchemeOutlineRequest:SearchSchemeByIdRequest = SearchSchemeByIdRequest(self)
    let loadSchemeOutlineSignal = PublishSubject<RXSignalElement>()
    
    private var rejectCallBacks:[String:AsmanLiteRejectCallBack] = [:]
    
    private var rtcAnswerCallBacks:[String:AsmanRtcAnswerCallBack] = [:]
    private lazy var rtcAnswerRequest:RtcAnswerRequest = {
        let request = RtcAnswerRequest(self)
        request.bodySource = self
        return request
    }()
    /// 方案
    private(set) var scheme:Scheme?
    
    init(withInitialParams params:InitialParams,token:String,sdkAppId:String,userSig:String) {
        self.userId = params.userId
        self.accountId = params.accountId
        self.roomId = params.roomId
        self.headUrl = params.headUrl
        self.name = params.name
        self.schemeId = params.schemeId
        self.vrType = params.vrType
        self.groupId = params.groupId
        self.token = token
        self.sdkAppId = sdkAppId
        self.userSig = userSig
        super.init()
    }
    func loadSchemeOutline(){
        self.loadSchemeOutlineRequest.loadData()
    }
    func reject(withCallBack callBack:AsmanLiteRejectCallBack?){
        let request = RejectRtcRequest(self)
        request.bodySource = self
        self.rejectCallBacks[request.szRequestId] = callBack
        request.loadData()
    }
    func rtcAnswer(callBack:AsmanRtcAnswerCallBack?){
        let request = RtcAnswerRequest(self)
        request.bodySource = self
        self.rejectCallBacks[request.szRequestId] = callBack
        self.rtcAnswerCallBacks[request.szRequestId] = callBack
        request.loadData()
    }
}

extension AsmanLiteViewModel : RequestCallBack, RequestDataReformer, RequestParamSource, RequestBodySource, RequestHeaderSource{
    func requestBaseUrl(_ request: BaseRequest) -> String? {
        return nil
    }
    func requestDidSucess(_ request: BaseRequest) {}
    
    func requestDidFail(_ request: BaseRequest) {
        if request.szRequestId == self.loadSchemeOutlineRequest.szRequestId {
            let message = request.errorMsg
            let callBack = RXSignalElement(success: false, message: message)
            self.loadSchemeOutlineSignal.onNext(callBack)
        }
        if request is RejectRtcRequest {
            let callBack = self.rejectCallBacks[request.szRequestId]
            self.rejectCallBacks[request.szRequestId] = nil
            let message = request.errorMsg
            callBack?(false,message)
        }
        if request is RtcAnswerRequest {
            let callBack = self.rtcAnswerCallBacks[request.szRequestId]
            self.rtcAnswerCallBacks[request.szRequestId] = nil
            let message = request.errorMsg
            callBack?(false,message)
        }
    }
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
//        if request.szRequestId == self.loadSchemeOutlineRequest.szRequestId {
//            guard let result = data?["result"] as? [String:Any] else {
//                let callBack = RXSignalElement(success: false, message: "数据异常")
//                self.loadSchemeOutlineSignal.onNext(callBack)
//                return
//            }
//            self.scheme = TrtcScheme(with: result)
//            let element = RXSignalElement(success: true, message: "")
//            self.loadSchemeOutlineSignal.onNext(element)
//        }
        if request is RejectRtcRequest {
            let callBack = self.rejectCallBacks[request.szRequestId]
            self.rejectCallBacks[request.szRequestId] = nil
            callBack?(true,nil)
        }
        if request is RtcAnswerRequest {
            let callBack = self.rtcAnswerCallBacks[request.szRequestId]
            self.rtcAnswerCallBacks[request.szRequestId] = nil
            callBack?(true,nil)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var params:[String:Any] = [:]
        if request.szRequestId == self.loadSchemeOutlineRequest.szRequestId {
            params["id"] = self.schemeId
            params["vrType"] = self.vrType
        }
        return params
    }
    
    func requestBody(_ request: BaseRequest) -> Any? {
        var body:[String:Any] = [:]
        if request is RejectRtcRequest {
            body["userId"] = self.userId
            body["accountId"] = self.accountId
            body["roomId"] = self.roomId
            body["schemeId"] = self.schemeId
            body["vrType"] = self.vrType
        }
        if request is RtcAnswerRequest {
            body["roomId"] = self.roomId
            body["schemeId"] = self.schemeId
            body["vrType"] = self.vrType
            body["groupId"] = self.groupId
        }
        return body
    }
    func requestHeader(_ request: BaseRequest) -> [String : Any]? {
        return [
            "x-access-token":self.token
        ]
    }
}
